# import math
# import random
#
# 《选择与循环》编程题
# 教材
# 3、编写程序,运行后用户输入4位整数作为年份,判断其是否为闰年。如果年份能400整除,则为间年:如果年份能被4整除但不能被100整除也为年。
# number = int(input("请输入数四位数："))
# if number % 4 == 0 and number % 100 != 0 or number % 400 == 0:
#     print("Yes,闰年")
# else:
#     print("No,平年")
# # text
# # 请输入数四位数：3000
# # No,平年
# # 请输入数四位数：2000
# # Yes,闰年
#
# 4、编写程序,生成一个包含50个随机整数的列表,然后删除其中所有奇数(提示:从后向前删)。
# integers = [random.randint(1, 100) for _ in range(50)]
# print(integers)
# # 方法一列表推导式
# evenList = [x for x in integers if x % 2 != 1]
# print(evenList)
# # 方法二使用filter
# evenList = list(filter(lambda x: x % 2 != 1, integers))
# print(evenList)
#
# [41, 10, 79, 50, 86, 50, 79, 28, 68, 54, 30, 100, 20, 80, 56, 40, 20, 50, 49, 28, 17, 52, 31, 6, 99, 31, 56, 9, 33, 21, 87, 40, 88, 87, 31, 55, 95, 3, 9, 13, 60, 97, 24, 38, 44, 97, 3, 3, 23, 48]
# [10, 50, 86, 50, 28, 68, 54, 30, 100, 20, 80, 56, 40, 20, 50, 28, 52, 6, 56, 40, 88, 60, 24, 38, 44, 48]
#
# 5、编写程序,生成一个包含20个随机整数的列表,然后对其中偶数下标的元素进行降序排列,奇数下标的元素不变(提示:使用切片)。
# integersList = [random.randint(1, 100) for _ in range(20)]
# print(integersList)
# evenList = integersList[::2]
# print(evenList)
# evenList.pop(0)  # 下标0不是奇数也不是偶数删除
# print(evenList)
# evenList.sort(reverse=True)
# print(evenList)
# needOne = 2
# needTow = 0
# while needOne < len(integersList):
#     integersList[needOne] = evenList[needTow]
#     needOne += 2
#     needTow += 1
# print(integersList)
#
# [11, 17, 79, 49, 28, 17, 66, 2, 9, 92, 71, 53, 80, 27, 14, 5, 55, 12, 36, 45]
# [11, 79, 28, 66, 9, 71, 80, 14, 55, 36]
# [79, 28, 66, 9, 71, 80, 14, 55, 36]
# [80, 79, 71, 66, 55, 36, 28, 14, 9]
# [11, 17, 80, 49, 79, 17, 71, 2, 66, 92, 55, 53, 36, 27, 28, 5, 14, 12, 9, 45]
#
# 6、编写程序,用户从键盘输入小于1000的整数,对其进行因式分解。例如10=2*5，60=2*2*3*5
# def zys(n):
#     print(n, "=", end='')
#     i = 2
#     while i * i <= n:
#         if n % i:
#             i += 1
#         else:
#             print(i, end='')
#             n //= i
#             if n > 1:
#                 print("*", end='')
#     if n > 1:
#         print(n, end="")
# n = int(input("请输入正整数:"))
# zys(n)
# 请输入正整数:60
# 60 =2*2*3*5
#
# 7、编写程序,至少使用两种不同的方法计算100以内所有奇数的和。
# numberList = list(range(0, 101))
# print(numberList)
# # 方法一使用函数
# numberList=numberList[1::2]
# print(numberList)
# numberSum = sum(numberList)
# print(numberSum)
# # 方法二直接循环累加
# numberSumOne = 0
# for i in numberList:
#     numberSumOne += i
# print(numberSumOne)
#
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
# 2500
# 2500
#
# 8.编写程序,输出所有由1、2、3、4这四个数字组成的素数,并且在每个素数中每个数字只使用一次。
# import itertools
# numbers = [1, 2, 3, 4]
# for r in range(1, len(numbers) + 1):  # r表示排列中元素的个数
#     permutations = itertools.permutations(numbers, r)  # 生成所有r个元素的排列
#     for perm in permutations:
#         perm = int(''.join(map(str, perm)))
#         print(perm,end=',')  # 输出排列
# 1, 2, 3, 4, 12, 13, 14, 21, 23, 24, 31, 32, 34, 41, 42, 43, 123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432, 1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321,
#
#
# 9.编写程序,实现分段函数计算：
# X             y
# X<0           0
# 0<=x<5        x
# 5<=x<10       3x-5
# 10<=x<20      0.5x-2
# 20<=x         0
#
# x = int(input("请输入x:"))
# if x < 0:
#     y = 0
# elif 0 <= x < 5:
#     y = x
# elif 5 <= x < 10:
#     y = 3 * x - 5
# elif 10 <= x < 20:
#     y = 0.5 * x - 2
# else:
#     y = 0
# print("y=", y)
# 请输入x:8
# y= 19
#
# 4、输出3位水仙花数。
# def flowerSum(num):
#     numStr = str(num)
#     n = len(numStr)  #数值长度
#     sum_of_powers = sum(int(digit)** n for digit in numStr)  #转换为字符串求和
#     return sum_of_powers == num
# for num in range(100,1001):
#     if flowerSum(num):
#         print(num)
# 153
# 370
# 371
# 407
#
# 5、统计考试中优秀、良好、中等、及格、不及格的人数。
# score_list = [random.randint(1, 100) for _ in range(100)]
# Y = 0
# L = 0
# Z = 0
# J = 0
# N = 0
# for i in score_list:
#     if i >= 90:
#         Y += 1
#     elif 80 <= i < 90:
#         L += 1
#     elif 70 <= i < 80:
#         Z += 1
#     elif 60 <= i < 70:
#         J += 1
#     else:
#         N += 1
# print("优秀：", Y, "良好：", L, "中等：", Z, "及格：", J, "不及格：", N, )
# 优秀： 14 良好： 10 中等： 7 及格： 9 不及格： 60
#
# 6、输入一个数，判断是否为素数。
# def price(i):
#     if i < 2:
#         return False
#     else:
#         for j in range(2, int(math.sqrt(i)) + 1):
#             if i % j == 0:
#                 return False
#         return True
#
# num = int(input("请输入一个数："))
# if price(num):
#     print("是素数")
# else:
#     print("不是素数")
#
# 7、计算前n个自然数的阶乘之和1!+2!+3!+……+n！
# num = int(input("请输入一个数："))
# num_list = [i for i in range(1, num + 1)]
# print(num_list)
# sum = 0
# for i in num_list:
#     k = 1
#     for j in range(1, i + 1):
#         k *= j
#     sum += k
# print("阶乘和为：", sum)
# 请输入一个数：5
# [1, 2, 3, 4, 5]
# 阶乘和为： 153
#
# 方法二
# num = int(input("请输入一个数："))
# num_list = [i for i in range(1, num + 1)]
# print(num_list)
# sum = 0
# for i in num_list:
#     k=math.factorial(i)
#     sum += k
# print("阶乘和为：", sum)
# 请输入一个数：4
# [1, 2, 3, 4]
# 阶乘和为： 33
#
# 8、e=1+1/1! + 1/2! + 1/3! + …… +1/n!，编写程序计算e的近似值。要求直到最后一项值小于10-8（表示为10e-8）输出最后一项的值及e的值。（提示：阶乘可用math库的math.factorial(x)求）
# e = 1
# i=1
# while i > 0:
#     k=1/(math.factorial(i))
#     if k <pow(10,-8):
#          break
#     e += k
#     i+=1
# print("e为：", e)
# e为： 2.718281826198493
#
# 9、1,2,3,5,8,13,21,34……，观察前面的数字，生成前100个数字组成的序列。
# denominator = 1
# molecule = 2
# molecule_one =1
# i = 0
# while molecule_one<100:
#     print(molecule_one, end=',')
#     molecule_one = molecule + denominator
#     denominator_one = molecule
#     molecule, denominator = molecule_one, denominator_one
# 1,3,5,8,13,21,34,55,89,
#
# 10、输出9x9的乘法表
# for i in range(1, 10):
#     for j in range(1, i + 1):
#         print("{}x{}={}\t".format(j, i, i * j), end="")
#     print()
# 1x1=1
# 1x2=2	2x2=4
# 1x3=3	2x3=6	3x3=9
# 1x4=4	2x4=8	3x4=12	4x4=16
# 1x5=5	2x5=10	3x5=15	4x5=20	5x5=25
# 1x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=36
# 1x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49
# 1x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64
# 1x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81
#
# 11、输入若干同学的计算机成绩，成绩分布在[0,100]区间，求出这些同学的计算机成绩的
# 平均值、最小值、最大值。每次提示是否继续输入，输入yes，继续输入，输入no停止输入。
# 输入在数字不在[0,100]区间，给出错误提示。
# def panduan():
#     judges = True
#     while judges:
#         index = input("是否继续输入:")
#         if index == "yes" or index == "Y" or index == "y":
#             judges = False
#             return True
#         elif index == "no" or index == "N" or index == "n":
#             judges = False
#             return False
#         else:
#             print("输入错误!")
#
# def jisuan(score_list):
#     aver = sum(score_list) / len(score_list)
#     mins = min(score_list)
#     maxs = max(score_list)
#     return aver, mins, maxs
#
# def inputscore():
#     judge = True
#     while judge:
#         score = int(input("请输入数字:"))
#         if 0 <= score <= 100:
#             score_list.append(score)
#             print(score_list)
#             tag = panduan()
#             if tag:
#                 judge = True
#             else:
#                 return score_list
#                 judge = False
#         else:
#             print("格式错误!")
#
# score_list = []
# score_list = inputscore()
# aver, mins, maxs = jisuan(score_list)
# print(f"平均成绩:{aver}")
# print(f"最低成绩:{mins}")
# print(f"最高成绩:{maxs}")
#
# 请输入数字:67
# [67]
# 是否继续输入:89
# 输入错误!
# 是否继续输入:y
# 请输入数字:56
# [67, 56]
# 是否继续输入:y
# 请输入数字:45
# [67, 56, 45]
# 是否继续输入:78
# 输入错误!
# 是否继续输入:y
# 请输入数字:23
# [67, 56, 45, 23]
# 是否继续输入:yes
# 请输入数字:67
# [67, 56, 45, 23, 67]
# 是否继续输入:n
# 平均成绩:51.6
# 最低成绩:23
# 最高成绩:67
#
# 12、猜数游戏：游戏开始，随机生成一个[0,100]区间的数字，用户输入一个数字猜测，如果用户输入大了，提示“大了”，如果用户输入小了，提示“小了”，最终计算并输出用户猜了多少次。
# number = random.randint(0, 100)
# print(number)
# sum = 0
# i = 1
# while i < 10:
#     x1 = eval(input("输入:"))
#     if x1 < number:
#         print("猜小!")
#         continue
#     elif x1 > number:
#         print("大了")
#         continue
#     else:
#         print("猜对了")
#         break
#     sum = sum+1
#
# 13、输出1000以内的素数，以及这些素数之和。（素数，是指除了1和该数本身之外，不能被其他任何整数整除的数）
# def price(i):
#     if i < 2:
#         return False
#     else:
#         for j in range(2, int(math.sqrt(i)) + 1):
#             if i % j == 0:
#                 return False
#         return True
#
# sum = 0
# for i in range(1, 1000):
#     if price(i):
#         sum += i
# print("素数和:", sum)
#
# 素数和: 76127
#
# 14、编写程序，按公式s=12+22+32+……+n2。求累加和s不超过1000的最大项数n。
# index = 12
# sum = 0
# tag = True
# while tag:
#     sum += index
#     index += 10
#     if sum > 1000:
#         print("最大项为:", index - 20)
#         sum = sum - (index - 10)
#         print("最大值:",sum)
#         tag = False
#
# 最大项为: 132
# 最大值: 936
#
# 15、编写程序，将由1、2、3、4这4个数字组成的每位数都不相同的所有三位数，存入列表中。
# import itertools
# numbers = [1, 2, 3, 4]
# permutations = itertools.permutations(numbers, 3)  # 生成所有r个元素的排列
# print(list(int(''.join(map(str, perm))) for perm in permutations))
# [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]
#
# 16、百钱买百鸡。公鸡5文钱一只，母鸡3文钱一只，小鸡3只一文钱，
# 用100文钱买一百只鸡,其中公鸡，母鸡，小鸡都必须要有，
# 问公鸡、母鸡、小鸡分别要买多少只刚好凑足100文钱。
# 初始化变量
# for x in range(1, 20):
#     for y in range(1, 33):
#         z = 100 - x - y
#         if z % 3 == 0 and 5*x + 3*y + z/3 == 100:
#             print(f"公鸡: {x} 只，母鸡: {y} 只，小鸡: {int(z)} 只，刚好100文钱。")
#             break
# 公鸡: 4 只，母鸡: 18 只，小鸡: 78 只，刚好100文钱。
# 公鸡: 8 只，母鸡: 11 只，小鸡: 81 只，刚好100文钱。
# 公鸡: 12 只，母鸡: 4 只，小鸡: 84 只，刚好100文钱。
#
# 17、鸡兔同笼问题。笼子里有若干只鸡和兔子，假设笼内鸡和兔的脚总数为80只。
# 问鸡和兔子各有多少只？已知每只鸡有2只脚，每只兔子有4只脚。
# for i in range(2,40):
# 鸡:32只，兔：4只；64+16=80只
#
# 18、编写一个求整数n阶乘（n!）的程序。
# def factorial(n):
#     if n == 0 or n == 1:
#         return 1
#     else:
#         return n * factorial(n - 1)
# n = int(input("请输入一个整数："))
# print(f"{n}的阶乘是：{factorial(n)}")
#
# 19、输入一个字符串，然后依次显示该字符串的每个字符，以及该字符的ASCII码。
# str = input("请输入字符串：")
# for i in str:
#     print(i,":",ord(i))
#
# d : 100
# f : 102
# j : 106
# s : 115
# n : 110
# d : 100
# f : 102
# j : 106
# n : 110
# s : 115
#
# 20、数字重复统计问题。（1）随机生成1000个整数，数字的范围[20,100]；
# （2）升序输出所有不同的数字，及其每个数字重复的次数。
# lists = [random.randint(20, 100) for _ in range(1000)]
# cf = dict([[i,lists.count(i)] for i in lists ])
# sorted_dict = dict(sorted(cf.items()))
# print(sorted_dict)
# {20: 8, 21: 15, 22: 14, 23: 19, 24: 9, 25: 9, 26: 8, 27: 11, 28: 18, 29: 15, 30: 15, 31: 9, 32: 13, 33: 14, 34: 12, 35: 11, 36: 11, 37: 11, 38: 10, 39: 14, 40: 10, 41: 14, 42: 17, 43: 13, 44: 13, 45: 11, 46: 15, 47: 19, 48: 9, 49: 10, 50: 9, 51: 14, 52: 22, 53: 13, 54: 17, 55: 16, 56: 11, 57: 10, 58: 13, 59: 16, 60: 5, 61: 7, 62: 11, 63: 16, 64: 13, 65: 9, 66: 9, 67: 12, 68: 15, 69: 13, 70: 13, 71: 13, 72: 12, 73: 13, 74: 12, 75: 7, 76: 17, 77: 8, 78: 15, 79: 8, 80: 12, 81: 16, 82: 8, 83: 12, 84: 13, 85: 11, 86: 17, 87: 14, 88: 13, 89: 13, 90: 10, 91: 9, 92: 14, 93: 8, 94: 15, 95: 22, 96: 8, 97: 8, 98: 18, 99: 11, 100: 2}
#
# 21、编写一个判断字符串是否是回文数。回文就是一个字符串从左到右读和从右到左读是完全一样的。例如，level,12321,ABA是回文。
# number_list = input("请输入：")
# index_first = 0
# i = 0
# tag = 0
# index_last = int(len(number_list))
# while i < int(len(number_list) / 2):
#     if number_list[index_first] == number_list[index_last - 1]:
#         index_first += 1
#         index_last -= 1
#         i += 1
#         tag = 1
#     else:
#         print("不是回文")
#         break
# if tag == 1:
#     print("是回文")
# 请输入：asdfghgfdsa
# 是回文
#
# 22、编写程序，求满足以下条件的最大的n值：
# 12+22+32+42+……+n2<1000
# index = 12
# sum = 0
# tag = True
# while tag:
#     sum += index
#     index += 10
#     if sum > 1000:
#         print("最大项为:", index - 20)
#         sum = sum - (index - 10)
#         print("最大值:",sum)
#         tag = False
#
# 最大项为: 132
# 最大值: 936
#
# 23、输入星号金字塔，要求打印n层。
# n = int(input("金字塔高度："))
# for i in range(1, n+1):
#     for j in range(0, n-i):
#         print(" ", end="")
#     for k in range(1, 2*i):
#         print("*", end="")
#     print()
#     金字塔高度：7
#       *
#      ***
#     *****
#    *******
#   *********
#  ***********
# *************
#
# 24、编写程序打印下图的字符金字塔，要求打印n层。
#       A
#      BAB
#     CBABC
#    DCBABCD
#   EDCBABCDE
#  FEDCBABCDEF
# GFEDCBABCDEFG
# def print_char_pyramid(n):
#     # 外层循环控制行数
#     for i in range(n):
#         # 打印空格 每增加一层,空格数量-1
#         print(' ' * (n - i - 1), end='')
#         # 打印左侧字母，从A开始
#         for j in range(i):
#             print(chr(65 + i - j), end='')
#         # 打印右侧字母，从倒数第二个开始
#         for j in range(i + 1):
#             print(chr(65 + j), end='')
#             # 换行
#         print()
#
# n = int(input("请输入金字塔的层数："))
# print_char_pyramid(n)
#
# 请输入金字塔的层数：4
#    A
#   BAB
#  CBABC
# DCBABCD
#
# 25、有四个数字：1、2、3、4，能组成多少个互不相同且无重复数字的三位数？各是多少？
# import itertools
# numbers = [1, 2, 3, 4]
# permutations = itertools.permutations(numbers, 4)  # 生成所有r个元素的排列
# print(list(int(''.join(map(str, perm))) for perm in permutations))
# [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]
#
# 26.判断101-200之间有多少个素数，并输出所有素数。
# def price(i):
#     if i < 2:
#         return False
#     else:
#         for j in range(2, int(math.sqrt(i)) + 1):
#             if i % j == 0:
#                 return False
#         return True
#
# su = []
# number = 0
# for j in range(101, 201):
#     k = price(j)
#     if k == True:
#         su.append(j)
#         number += 1
# print("数量：", number, "是：", su)
# 数量： 21 是： [101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
#
# 27、将一个正整数分解质因数。例如：输入90,打印出90=2*3*3*5。
# def zys(n):
#     print(n, "=", end='')
#     i = 2
#     while i * i <= n:
#         if n % i:
#             i += 1
#         else:
#             print(i, end='')
#             n //= i
#             if n > 1:
#                 print("*", end='')
#     if n > 1:
#         print(n, end="")
# n = int(input("请输入正整数:"))
# zys(n)
#
# 请输入正整数:90
# 90 =2*3*3*5
#
# 28、输入一行字符，分别统计出其中英文字母、空格、数字和其它字符的个数。
# def count_chars(input_string):
#     # 初始化计数器
#     letters = 0
#     spaces = 0
#     digits = 0
#     others = 0
#
#     # 遍历字符串中的每个字符
#     for char in input_string:
#         # 检查字符是否是英文字母
#         if char.isalpha():
#             letters += 1
#             # 检查字符是否是空格
#         elif char.isspace():
#             spaces += 1
#             # 检查字符是否是数字
#         elif char.isdigit():
#             digits += 1
#             # 如果都不是，则属于其他字符
#         else:
#             others += 1
#     return letters, spaces, digits, others
# input_line = input("请输入一行字符：")
# letters, spaces, digits, others = count_chars(input_line)
# print(f"英文字母个数：{letters}")
# print(f"空格个数：{spaces}")
# print(f"数字个数：{digits}")
# print(f"其他字符个数：{others}")
#
# 请输入一行字符：1234567!@#$%^SDFG  FGHJ45678HGF//.,;'[]\
# 英文字母个数：11
# 空格个数：2
# 数字个数：12
# 其他字符个数：15
#
# 29.求1+2!+3!+...+20!的和。
# num_list = [i for i in range(1, 21)]
# print(num_list)
# sum = 0
# for i in num_list:
#     k = 1
#     for j in range(1, i + 1):
#         k *= j
#     sum += k
# print("阶乘和为：", sum)
#
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# 阶乘和为： 2561327494111820313
#
# 方法二
# num_list = [i for i in range(1, 21)]
# print(num_list)
# sum = 0
# for i in num_list:
#     k=math.factorial(i)
#     sum += k
# print("阶乘和为：", sum)
#
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# 阶乘和为： 2561327494111820313
#
# 30、一个5位数，判断它是不是回文数。即12321是回文数，个位与万位相同，十位与千位相同。
# number_list = input("请输入：")
# index_first = 0
# i = 0
# tag = 0
# index_last = int(len(number_list))
# while i < int(len(number_list) / 2):
#     if number_list[index_first] == number_list[index_last - 1]:
#         index_first += 1
#         index_last -= 1
#         i += 1
#         tag = 1
#     else:
#         print("不是回文")
#         break
# if tag == 1:
#     print("是回文")
#
# 请输入：45678987654
# 是回文
#
# 31.（选做）排序算法很多，请理清冒泡排序、插入排序、选择排序的思路，用Python列表完成以上排序算法。
# list = [random.randint(0, 1000) for i in range(0, 10)]
# print("list：",list)
# [195, 148, 99, 65, 460, 89, 349, 951, 880, 20]
# 冒泡排序（两两交换）
# k = len(list)-1
# for j in range(0, len(list) - 1):
#     for i in range(0, k):
#         if list[i] > list[i+1]:
#             temp = list[i]
#             list[i] = list[i+1]
#             list[i + 1] = temp
#         i += 1
#     k -= 1
# print(list)
# [53, 125, 178, 294, 728, 730, 730, 823, 883, 943]
#
# 插入排序
# for i in range(1, len(list)):
#     for j in range(i, 0, -1):
#         if list[j] < list[j - 1]:
#             list[j], list[j - 1] = list[j - 1], list[j]
# print(list)
#
#
